import { Component, Vue } from 'vue-property-decorator'
import { IvrModule } from '@/store/modules/config/ivr'
import { ElMessage } from 'element-ui/types/message'
import { CtiModule } from '@/store/modules/cti/index'

@Component({
  name: 'task-mixin',
  filters: {
    getTaskStatus (item) {
      let text = '未知状态'
      switch (item.status) {
        case 'notTurnedOn':
          text = '未开启'
          break;
        case 'dealing':
          text = '执行中'
          break;
        case 'pauseding':
          text = '暂停中'
          break;
        case 'paused':
          text = '已暂停'
          break;
        case 'complete':
          text = '已完成'
          break;
        case 'archived':
          text = '已归档'
          break;
        default:
          break;
      }
      return text
    }
  }
})

export default class extends Vue {
  public $bus: any
  public $message: undefined | ElMessage
  public $store: any

  get account () {
    return (this.$store as any).state.common.session.account._id
  }

  get userId () {
    return (this.$store as any).state.common.session.user._id
  }

  // get agentNumber () {
  //   return (this.$store as any).state.common.session.user.agentNumber
  // }

  get loginToken () {
    return (this.$store as any).state.common.session.user.loginToken
  }

  /**
   * 执行率
   * @param row.called {number} 呼叫次数
   * @param row.total {number} 任务数量
   * @returns {number}
   */
  public getProcess (row: any): number {
    const num = parseFloat(row.called);
    const total = parseFloat(row.total);
    if (isNaN(num) || isNaN(total)) {
      return 0
    }
    return total <= 0 ? 0 : (Math.round(num / total * 10000) / 100.00)
  }

  public getFormat (row: any) {
    return row.called + '/' + row.total
  }

  /**
   * 接通率
   * @param row.dealing {number} 接听数量
   * @param row.callout {number} 呼叫次数
   * @returns {number}
   */
  public getConnectionRate (row: any) {
    const num = parseFloat(row.dealing);
    const total = parseFloat(row.callout);
    if (isNaN(num) || isNaN(total)) {
      return '-'
    }
    return total <= 0 ? 0 : (Math.round(num / total * 10000) / 100.00)
  }

  /**
   * 删除任务
   * @param _id {string} 任务id
   * @return {promise}
   */
  public handleDelete (id: string) {
    return new Promise((resolve, reject) => {
      const tipText = '删除'
      const query = { ids: [id] }
      const data = { url: '/call/action/autoCallTask/batch/delete', method: 'POST', data: query }
      IvrModule.IvrCommonAction(data).then((res: any) => {
        if (res.success) {
          (this.$message as ElMessage).success(`${tipText}成功`)
          resolve(true)
        } else {
          resolve(false)
        }
      })
    })
  }

  /**
   * 更新任务状态
   * @param id {string} 任务id
   * @param status {string} 更新的状态
   * @return {promise}
   */
  public updateTaskState (id: string, status: string) {
    return new Promise((resolve) => {
      let tipText = ''
      const query = { _id: id, status }
      const data = { url: '/call/action/autoCallTask/update/status', method: 'POST', data: query }
      if (status === 'turnedOn') {
        tipText = '开启'
      }
      if (status === 'paused') {
        tipText = '暂停'
      }
      IvrModule.IvrCommonAction(data).then((res: any) => {
        if (res.success) {
          (this.$message as ElMessage).success(`${tipText}成功`);
          resolve(res)
        }
      })
    })
  }

  public handleSetTabelHeight () {
    this.$nextTick(() => {
      (this.$refs as any).configTabel.handleSetTabelHeight()
    })
  }
}
